﻿using Ducu.GLB.Global;
using Ducu.UI.Responsible.Controllers;
using Ducu.UI.Responsible.WebHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Ducu.UI.Responsible.Filter
{
    public class NeedLogOn : AuthorizeAttribute
    {
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            PrepageUrlHelper helper = (filterContext.Controller as BaseController).Prepage;

            if (HttpContext.Current.Request.Cookies[CookieName.USER_ID] == null)
            {
                string url = helper.GetRefuseAccess();

                //TODO: don't know why this code is necessary, 
                //else cause 401 error when deploying on IIS7.5
                HttpContext.Current.Response.StatusCode = 302;
                filterContext.Result = new RedirectResult(url);
            }
        }
    }
}